package com.molyfun.parents.activity.common;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.StringResponse;
import com.molyfun.parents.rest.bean.UploadUrlRequest;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.UrlUtils;
import com.molyfun.parents.view.media.AndroidMediaController;
import com.molyfun.parents.view.media.IjkVideoView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import tv.danmaku.ijk.media.player.IMediaPlayer;


public class VideoPlayerActivity extends BaseActivity implements View.OnClickListener, IMediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, View.OnTouchListener {

    @InjectView(R.id.video_view)
    IjkVideoView mMediaPlayer;
    @InjectView(R.id.base_title_back)
    ImageView mTitleBack;
    @InjectView(R.id.base_title)
    TextView mTitle;

    private AndroidMediaController mMediaController;


    private String mVideoTitle = "";
    private String mVideoUrl = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        ButterKnife.inject(this);
        initIntent();
        mTitleBack.setOnClickListener(this);
    }

    public static void startActivity(Context context, String title, String url) {
        Intent intent = new Intent(context, VideoPlayerActivity.class);
        intent.putExtra(Const.FLAG_VIDEO_TITLE, title);
        intent.putExtra(Const.FLAG_VIDEO_URL, url);
        context.startActivity(intent);
    }

    private void initIntent() {
        Intent intent = getIntent();
        mVideoTitle = intent.getStringExtra(Const.FLAG_VIDEO_TITLE);
        mTitle.setText(mVideoTitle);
        mVideoUrl = intent.getStringExtra(Const.FLAG_VIDEO_URL);
//        mMediaController = new AndroidMediaController(this, false);
//        mMediaPlayer.setMediaController(mMediaController);
        if (!TextUtils.isEmpty(mVideoUrl)) {

            if (UrlUtils.isExpired(mVideoUrl)) {
                showProgressDialog();
                UploadUrlRequest uploadUrlRequest = new UploadUrlRequest();
                uploadUrlRequest.setUrl(UrlUtils.getNewUrl(mVideoUrl));
                Call<StringResponse> call = MFPApplication.getInstance().getRestClient().updateMediaUrl(uploadUrlRequest);
                call.enqueue(new Callback<StringResponse>() {
                    @Override
                    public void onResponse(Call<StringResponse> call, Response<StringResponse> response) {
                        dismissProgressDialog();
                        mMediaPlayer.setVideoPath(response.body().getData());
                        mMediaPlayer.start();
                    }

                    @Override
                    public void onFailure(Call<StringResponse> call, Throwable t) {
                        dismissProgressDialog();
                        toast("获取视频地址失败");
                    }
                });
            } else {
                mMediaPlayer.setVideoPath(mVideoUrl);
                mMediaPlayer.start();
            }
        }
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        finish();
        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mMediaPlayer.start();
    }

    @Override
    public void onCompletion(IMediaPlayer mp) {
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_back:
                finish();
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        mMediaPlayer.stopPlayback();
//        mMediaPlayer.release(true);
//        mMediaPlayer.stopBackgroundPlay();
//        IjkMediaPlayer.native_profileEnd();
    }
}
